package stuman.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import stuman.activity.Login;
import stuman.activity.OpenPage;
import stuman.entity.Student;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SaveLoad {

	public static void save(Context c){
		FileOutputStream fos;
		ObjectOutputStream oos;
		
		try{
			
			fos = c.openFileOutput("test.txt", c.MODE_PRIVATE);		
			oos = new ObjectOutputStream(fos);	
			
			for (int i=0;i<Container.students.size();i++){		
				oos.writeObject(Container.students.get(i));
				//Student stu = new Student("First", "Last");
				//Toast.makeText(c, Container.students.get(i).getFirstName(), Toast.LENGTH_LONG).show();
				//oos.writeObject(Container.students.get(i));
				
			}
			//Toast.makeText(getApplicationContext(), "data saved", Toast.LENGTH_LONG).show();
			oos.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//Toast.makeText(c, e.toString(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}
	
	
	public static void load(Context c){
		FileInputStream fis;
		ObjectInputStream ois;
		
		try{
			fis = c.openFileInput("test.txt");
			ois = new ObjectInputStream(fis);
			
			Container.students.clear();
			Container.students.add((Student) ois.readObject());
			
			ois.close();
			fis.close();
					
		}
		
		catch (IOException e) {
			Toast.makeText(c, e.toString(), Toast.LENGTH_LONG).show();	
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			Toast.makeText(c, e.toString(), Toast.LENGTH_LONG).show();
		}
	}
	
	
}
